"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../config");
const infoerr_1 = require("../router/middleware/infoerr");
const UserSer_1 = __importDefault(require("../services/UserSer"));
function default_1(req, res, next) {
    return __awaiter(this, void 0, void 0, function* () {
        res.setHeader("Access-Control-Allow-Origin", "*");
        const istoken = config_1.tokenPath.some((item) => req.path === item.path && req.method === item.method);
        if (!istoken)
            return next();
        const token = req.signedCookies.feifan;
        if (!token)
            return (0, infoerr_1.err)(res, "没有token");
        if (!token.name || !token.id)
            return (0, infoerr_1.err)(res, "toke有误");
        const result = yield UserSer_1.default.findistoken(token);
        if (!result)
            return (0, infoerr_1.err)(res, "token信息错误", { 解决: "如果近期修改过密码请重新登录" });
        if (req.url === "/api/comment/submit" && req.method === "POST") {
            req.query = result;
            console.log(result);
        }
        if (req.path === "/api/user/islogin")
            return (0, infoerr_1.info)(res, "ok", result);
        console.log(`用户${token.name} 访问了:${req.path}`);
        next();
    });
}
exports.default = default_1;
